home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / Fudd Source / Fudd ƒ / Fudd code ƒ / fudd file management.c < prev    next >
Text File  |  1994-02-06  |  4KB  |  181 lines

  1. /**********************************************************************\
  2.  
  3. File:        fudd file management.c
  4.  
  5. Purpose:    This module handles creating, setting up, and deleting
  6.             temp files, opening and reading the source file, and
  7.             opening and writing the dest file.
  8.  
  9.  
  10. Fudd -=- convert text to Elmer Fudd talk
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "Script.h"
  31. #include "program globals.h"
  32. #include "fudd file management.h"
  33. #include "msg environment.h"
  34. #include "util.h"
  35.  
  36. FSSpec            inputFS, outputFS, tempFS;
  37. Boolean            deleteTheThing;
  38. int                inputRefNum, outputRefNum;
  39. unsigned long    gWhatsReallyInInputBuffer;
  40. unsigned long    gTotalOutputLength;
  41.  
  42. void InitFiles(void)
  43. {
  44.     inputRefNum=outputRefNum=0;
  45.     gWhatsReallyInInputBuffer=gTotalOutputLength=0L;
  46. }
  47.  
  48. int OpenInputFile(void)
  49. {
  50.     OSErr            theError;
  51.     
  52.     if (gHasFSSpecs)
  53.         theError=FSpOpenDF(&inputFS, fsRdPerm, &inputRefNum);
  54.     else
  55.         theError=HOpen(inputFS.vRefNum, inputFS.parID, inputFS.name, fsRdPerm, &inputRefNum);
  56.     
  57.     if (theError==noErr)
  58.         GetEOF(inputRefNum, &gInputLength);
  59.     
  60.     return (theError==noErr) ? allsWell : kCantOpenInputFile;
  61. }
  62.  
  63. int CreateTempFile(void)
  64. {
  65.     int                    i;
  66.     Str255                timeStr;
  67.     OSErr                isHuman;
  68.     unsigned long        fileType;
  69.     int                    resultCode;
  70.     
  71.     tempFS=outputFS;
  72.     tempFS.name[0]=0x05;
  73.     tempFS.name[1]='b';
  74.     tempFS.name[2]='o';
  75.     tempFS.name[3]='r';
  76.     tempFS.name[4]='k';
  77.     tempFS.name[5]='.';
  78.     NumToString(Time&0x7fffffff, timeStr);
  79.     for (i=1; i<=timeStr[0]; i++)
  80.         tempFS.name[++(tempFS.name[0])]=timeStr[i];
  81.     
  82.     if (gHasFSSpecs)
  83.         isHuman=FSpCreate(&tempFS, CREATOR, 'TeMp', smSystemScript);
  84.     else
  85.         isHuman=HCreate(tempFS.vRefNum, tempFS.parID, tempFS.name, CREATOR, 'TeMp');
  86.     
  87.     return (isHuman==noErr) ? allsWell : kCantCreateTempFile;
  88. }
  89.  
  90. int SetupTempFile(void)
  91. {
  92.     OSErr                isHuman;
  93.     unsigned long        chefLen;
  94.     
  95.     if (gHasFSSpecs)
  96.         isHuman=FSpOpenDF(&tempFS, fsRdWrPerm, &outputRefNum);
  97.     else
  98.         isHuman=HOpen(tempFS.vRefNum, tempFS.parID, tempFS.name, fsRdWrPerm, &outputRefNum);
  99.     
  100.     if (isHuman!=noErr)
  101.         return kCantOpenTempFile;
  102.         
  103.     SetEOF(outputRefNum, 0L);
  104.     SetFPos(outputRefNum, 1, 0L);
  105.  
  106.     return allsWell;
  107. }
  108.  
  109. void FinalizeFiles(Boolean good)
  110. {
  111.     FInfo                theInfo;
  112.     
  113.     if (inputRefNum)
  114.         FSClose(inputRefNum);
  115.     if (outputRefNum)
  116.     {
  117.         SetEOF(outputRefNum, gTotalOutputLength);
  118.         FSClose(outputRefNum);
  119.     }
  120.     FlushVol(0L, outputRefNum);
  121.     
  122.     if (!good)
  123.     {
  124.         if(gHasFSSpecs)
  125.             FSpDelete(&tempFS);
  126.         else
  127.             HDelete(tempFS.vRefNum, tempFS.parID, tempFS.name);
  128.         
  129.         return;
  130.     }
  131.     
  132.     if (deleteTheThing)
  133.     {
  134.         if (gHasFSSpecs)
  135.             FSpDelete(&outputFS);
  136.         else
  137.             HDelete(outputFS.vRefNum, outputFS.parID, outputFS.name);
  138.     }
  139.     FlushVol(0L, outputRefNum);
  140.         
  141.     if (gHasFSSpecs)
  142.         FSpGetFInfo(&inputFS, &theInfo);
  143.     else
  144.         HGetFInfo(inputFS.vRefNum, inputFS.parID, inputFS.name, &theInfo);
  145.  
  146.     theInfo.fdFlags&=~0x0100;    /* clear Inited bit */
  147.     
  148.     if (gHasFSSpecs)
  149.         FSpSetFInfo(&tempFS, &theInfo);
  150.     else
  151.         HSetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name, &theInfo);
  152.     FlushVol(0L, outputRefNum);
  153.     
  154.     if (gHasFSSpecs)
  155.         FSpRename(&tempFS, outputFS.name);
  156.     else
  157.         HRename(tempFS.vRefNum, tempFS.parID, tempFS.name, outputFS.name);
  158.     FlushVol(0L, outputRefNum);
  159. }
  160.  
  161. int ReadInputFile(int thisFile, unsigned char* buffer, unsigned long count)
  162. {
  163.     OSErr            isHuman;
  164.     unsigned long    temp;
  165.     
  166.     temp=count;
  167.     isHuman=FSRead(thisFile, &temp, buffer);
  168.     return ((isHuman==eofErr) || (isHuman==noErr)) ? allsWell : kCantReadInputFile;
  169. }
  170.  
  171. int WriteTempFile(int thatFile, unsigned char* buffer, unsigned long theLength)
  172. {
  173.     unsigned long    oldFPos;
  174.     
  175.     gTotalOutputLength+=theLength;
  176.     GetFPos(thatFile, &oldFPos);
  177.     SetEOF(thatFile, gTotalOutputLength);
  178.     SetFPos(thatFile, 1, oldFPos);
  179.     return (FSWrite(thatFile, &theLength, buffer)==noErr ? allsWell : kCantWriteTempFile);
  180. }
  181.